package com.cnpc.ds;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;
import java.util.Scanner;

/**
 * @author maqh
 * @date 2019/9/6 16:41
 */
public class Generator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.nextLine();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的 " + tip + "！");
    }

    public static void main(String[] args) {
        String moduleComment = Generator.scanner("模块中文描述");
        String tableName = Generator.scanner("表全称(大小写无所谓)");
        String projectPath = System.getProperty("user.dir");

        DataSourceConfig dsc = new DataSourceConfig
                .Builder("jdbc:postgresql://11.53.75.57:5432/seresdrill",
                "seresdrill",
                "Nwu0ugy1u28V5")
                .build();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc);

        String author = "作者";//Generator.scanner("作者 例子 jyq");
        //项目路径+ 模块名称 + src
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig
                .Builder()
                .outputDir(projectPath + "/src/main/java")
                .author(author)
                .disableOpenDir()
                .fileOverride()  //覆盖已有文件
                .build();

//        moduleName = moduleName.replace("maasp_", "");

        // 包配置
        PackageConfig packageConfig = new PackageConfig
                .Builder()
                .parent("com.cnpc.ds")
                .build();

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig
                .Builder()
                .mapperXml("")
                .controller("templates/controller.java")
                .service("templates/service.java")
                .serviceImpl("templates/serviceImpl.java")
                .mapper("templates/mapper.java")
                .entity("templates/entity.java")
                .build();

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig
                .Builder()
//                .addInclude(scanner("表名，支持多表").split(","))
                .addInclude(tableName.toLowerCase())
                //过滤规则
                .addTablePrefix("ED_")
                //逻辑删除
                .entityBuilder().logicDeleteColumnName("bsflag").logicDeletePropertyName("bsflag")
                .addTableFills(new Column("CREATE_DATE", FieldFill.INSERT)) // 表字段填充
                .addTableFills(new Column("UPDATE_DATE", FieldFill.UPDATE)) // 表字段填充
                .addTableFills(new Column("CREATE_USER", FieldFill.INSERT)) // 表字段填充
                .addTableFills(new Column("UPDATE_USER", FieldFill.UPDATE)) // 表字段填充
                .idType(IdType.ASSIGN_ID)
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel)
                .enableLombok()
                .controllerBuilder()
                .enableRestStyle()
                .build();


        //自定义配置参数
        InjectionConfig injectionConfig = new InjectionConfig.Builder()
                .beforeOutputFile((tableInfo, objectMap) -> {
                    System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                })
                .customMap(Collections.singletonMap("moduleComment", moduleComment))
                .build();
        mpg.global(globalConfig);
        mpg.packageInfo(packageConfig);
        mpg.template(templateConfig);
        mpg.strategy(strategyConfig);
        mpg.injection(injectionConfig);
        mpg.execute(new FreemarkerTemplateEngine());
    }
}
